# -*- coding:utf-8 -*-
import tkinter as tk
import smtplib
from tkinter import ttk
from tkinter import messagebox
from email.mime.text import MIMEText
from email.header import Header
from_addr = '***@126.com'
password = '******'
to_addr = '***@qq.com'
smtp_server = 'smtp.126.com'
root_window =tk.Tk()
root_window.title('第一个用Python编写的带有GUI界面的发邮件程序')
root_window.geometry('500x200')
root_window["background"] = "#acf6ef"
text=tk.Label(root_window,text="欢迎使用本软件",bg="#acf6ef",fg="black",font=('Times', 20, 'bold'))
text.pack()
text1=tk.Label(root_window,text="在下方输入框里输入您想说的话，然后点击按钮，作者就会收到您的邮件了",bg="#acf6ef",fg="black",font=('微软雅黑', 10))
text1.pack()
entry1 = tk.Entry(root_window,width=180)
entry1.pack(padx=20, pady=20)
a=entry1.get()
def click_button():
    msg = MIMEText(entry1.get(), 'plain', 'utf-8')
    msg['From'] = Header('Python')
    msg['To'] = Header('******@qq.com')
    subject = '来自Python的邮件'
    msg['Subject'] = Header(subject, 'utf-8')
    try:
        
        smtpobj = smtplib.SMTP_SSL(smtp_server)
        smtpobj.connect(smtp_server, 465)    
        smtpobj.login(from_addr, password)   
        smtpobj.sendmail(from_addr, to_addr, msg.as_string())
        messagebox.showinfo(title='恭喜您', message='邮件发送成功')
    except smtplib.SMTPException:
        messagebox.showinfo(title='很遗憾', message='邮件发送失败')
    finally:
        smtpobj.quit()
button = tk.Button(root_window,text='发送邮件',bg='#2ae0c8',width=10, height=2,command=click_button).pack()
root_window.mainloop()
